package com.itheima.es;

import com.alibaba.fastjson.JSON;
import com.itheima.es.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 演示: 操作es客户端
 * 文档的CRUD
 * @作者 itcast
 * @创建日期 2021/3/19 9:09
 **/
public class EsDemo02 {
    RestHighLevelClient client;

    /**
     * 添加文档
     */
    @Test
    public void addDocument() throws IOException {
        // 要添加的文档数据
        User user = new User(1L,"鲁班",18,"1","鲁班在河道唱着歌");
        // 1. 创建对应请求对象
        IndexRequest request = new IndexRequest("user");// 索引库名称
        // 1.1 设置文档的id
        request.id(user.getId() + "");
        // 1.2 将文档内容设置到request中
        // 参数1: 文档内容json字符串   参数2: 内容格式
        String userJson = JSON.toJSONString(user);
        request.source(userJson,XContentType.JSON);
        // 2. 执行请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 3. 解析请求
        System.out.println(response.getResult());
    }

    @Test
    public void findDocById() throws IOException {
        // 1. 创建对应请求对象
        GetRequest request = new GetRequest("user","1"); // 索引库   文档ID
        // 2. 执行请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3. 解析请求
        String userJson = response.getSourceAsString();// 获取到文档的json字符串
        User user = JSON.parseObject(userJson, User.class);
        System.out.println(user);
    }

    @Test
    public void deleteById() throws IOException {
        // 1. 创建对应请求对象
        DeleteRequest request = new DeleteRequest("user","1");
        // 2. 执行请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        // 3. 解析请求
        System.out.println(response.getResult());
    }

    @Test
    public void bulkList() throws IOException {
        // 需要导入的集合数据
        List<User> userList = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱")
        );

        // 1. 创建对应请求对象 (BulkRequest)
        BulkRequest request = new BulkRequest("user");
        // 1.1  循环遍历要导入的集合数据
        for (User user : userList) {
            // 1.2  遍历的每一个元素  都对应的创建一个 IndexRequest
            IndexRequest indexRequest = new IndexRequest();
            // 设置文档ID   设置文档内容
            indexRequest.id(user.getId()+"");
            indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
            // 1.3 将IndexRequest请求对象 添加到 BulkRequest中
            request.add(indexRequest);
        }
        // 2. 执行请求 client.bulk 批处理
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
        // 3. 解析请求
        System.out.println(bulk.status());
    }




    @Before
    public void init(){
        // 创建操作es的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.129",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // 创建操作es的客户端
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.200.129",9200,"http")
//                )
//        );
//        // 使用client操作es
//
//        // 使用完毕后需要释放链接
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
